﻿using UnityEngine;
using System.Collections;

public class PingPongX : MonoBehaviour
{
	public float runSpeed;
	
	public bool itemRunUp = false;
	
	void Start ()
	{

	}
	
	void Update ()
	{
		Vector3 myTransform = transform.position;

		if (Input.GetKey ("e") && runSpeed == 0) {
						runSpeed = 0.25f;
			StartCoroutine ("itemRun", 3);
				} 
		
		if (itemRunUp == true)
		{
			myTransform.x += runSpeed;
			transform.position = myTransform;
		}
		
		else if (itemRunUp == false)
		{
			myTransform.x -= runSpeed;
			transform.position = myTransform;
		}
	}
	
	IEnumerator itemRun (float repeatAfter)
	{
		int i;
		for (i=1; i>0; i++)
		{
			if (itemRunUp)
			{
				itemRunUp = false;
				yield return new WaitForSeconds (repeatAfter);
			}
			else if (itemRunUp == false)
			{
				itemRunUp = true;
				yield return new WaitForSeconds (repeatAfter);
			}
		}
	}
}